home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / khds.doc < prev    next >
Text File  |  1994-05-30  |  18KB  |  626 lines

  1.                  KNOW-HOW.DataShell
  2.                (Paradox Engine Shell)
  3. (C) Stepan S.Vartanov, 1994                                  
  4.  
  5.      KNOW-HOW.DATASHELL is a Borland Paradox Engine 3.01 shell. 
  6. It consists of 3 parts. 
  7. 1. Abstract table representation. Viewer - Editor for any tables (DB,
  8. Spreadsheet and so on) could be derived from it by overloading the
  9. virtual field access functions.
  10. 2. Paradox tables viewer - editor - ask manager (as one of (1)).
  11. 3. QBE Manager (window - independent) for Paradox tables.
  12.  
  13. ATTENTION !!! First of all it is tables representation, Paradox 
  14. Engine Shell is only one of possible adaptations.
  15.  
  16. Tested on Borland C++ 3.x.
  17.  
  18. //////////////////////////////////////////////////////////////////////////
  19.     Field incapsulation (types are listed in FLDTYPE.H, this set         / 
  20. may be expanded):                                                        /
  21.  
  22. enum KH_FIELD_TYPE { KH_NUMBER = 1, KH_STRING = 2,
  23.                KH_DATE = 4,  KH_SHORT = 8, KH_MONEY = 16,
  24.                KH_BLOB = 32, KH_ALL = 63 };
  25.  
  26. PXFIELD.H:
  27.  
  28. #include <pxengine.h>
  29. #include "khpxeerr.h"
  30. #include "fldtype.h"
  31.  
  32. struct KH_PXFLD
  33.     {
  34.     KH_FIELD_TYPE type;  // Field type
  35.  
  36.      char* str;          // Data
  37.      double n;
  38.      long date;
  39.      short sh;
  40.  
  41.     KH_PXFLD(double f)   { type = KH_NUMBER; n = f; }
  42.     KH_PXFLD(long f)     { type = KH_DATE; date = f; }
  43.     KH_PXFLD(short f)    { type = KH_SHORT; sh = f; }
  44.     KH_PXFLD(char* buf)  { type = KH_STRING, str = buf; }
  45.     };
  46.  
  47.     File "khpxeerr.h" registers few additional error messages:
  48.  
  49. char* KH_ERRORS[] = {
  50.      "",
  51.      "No checked fields in querry",
  52.      "Table is not linked with others. Use EXAMPLE",
  53.      "Incompatible field types with the same EXAMPLE"
  54. You could not querry BLOBS (DataShell could not, YOU could):
  55.      "Condition on BLOB field",
  56.      "Example on BLOB field"
  57.      };
  58.  
  59.     Table incapsulation, PX_TABLE.H.
  60. class AskManager;
  61. class PrintManager;
  62.  
  63. class KH_PXTable
  64.     {
  65.     protected:
  66.      char* fileName;                        // Table Name
  67.      TABLEHANDLE tblHandle;                 // Table Handle
  68.      RECORDHANDLE recHandle;                // Record Handle
  69.      FIELDHANDLE fldHandle;                 // Field Handle
  70.      RECORDNUMBER current;                  // Current record number
  71.      RECORDNUMBER nRecords;                 // Total length
  72.      int nFlds;                             // Fields in table
  73.  
  74.     public:
  75.      KH_PXTable(char* fName, int idx = 0, // Index, simple or complex
  76. If new table created:
  77.           int fNo = 0,              
  78.           char** fields = NULL,     
  79.           char** types = NULL);     
  80.      ~KH_PXTable();
  81. // Service
  82.      TABLEHANDLE getTblHandle() { return tblHandle; }
  83.      RECORDHANDLE getRecHandle() { return recHandle; }
  84.      FIELDHANDLE getFldHandle() { return fldHandle; }
  85.  
  86.      void setFld(int f) { fldHandle = f; } 
  87.      void setRec(RECORDHANDLE rec) { recHandle = rec; }
  88.      int getField(KH_PXFLD* ret);
  89.      int putField(KH_PXFLD* f);
  90. Is querry correct? Return 0 (FALSE), 1 (TRUE).
  91.      int testQuerry(char** shablon);
  92. Convert field to string (char*).
  93.      KH_FIELD_TYPE TranslateField(char* buf);
  94. Single table querry. Use shablon - set of field querries. Methods of
  95. comparing could be overloaded in child class.
  96.      int Find(char** shablon);
  97.  
  98.      virtual int Compare(char* shab, char* field);  // Strings
  99.      virtual int Compare(char* shab, double field); // Numbers
  100.      virtual int Compare(char* shab, long field);   // Date and Long
  101.      virtual int Compare(char* shab, short field);  // Short
  102.  
  103.     friend class KH_AskManager;
  104.     friend class KH_ReportManager;
  105.     };
  106.  
  107.     Example.
  108.  
  109. #include <iostream.h>
  110. #include <dos.h>
  111.  
  112. /////////////////////
  113. void main()
  114.     {
  115.     if(PXInit() != PXSUCCESS)
  116.      return;
  117.  
  118.     KH_PXTable table("demo.db");
  119.  
  120.     char* shablon[] = { "", "", "", "Ivanov", "", "", "", "", "" };
  121.     char buf[255];
  122.     int eot;
  123.     while(table.Find(shablon) == 1)
  124.      {
  125.      for(int i = 1; i < 5; i++)
  126.          {
  127.          table.setFld(i);
  128.          table.TranslateField(buf);
  129.          cout << buf;
  130.          }
  131.      cout << "\n";
  132.      }
  133.     PXExit();
  134.     }
  135.  
  136.     AskManager class work with multy tables querries. To link tables
  137. EXAMPLES are used.
  138.  
  139.     STRTABLE.H:
  140.  
  141. struct KH_STRTABLE
  142.     {
  143.     int used;           
  144.     int total;          
  145.     char** strings;     
  146.     KH_STRTABLE(int n, char** str = NULL);
  147.     ~KH_STRTABLE();             
  148.     int add(char* str);         
  149.     char* operator [](int n) { return strings[n]; }
  150.     };
  151.  
  152.      KHQUERRY.H incapsulate querry (examples, checks, ...):
  153.  
  154. struct KH_QUERRY
  155.     {
  156.     KH_STRTABLE* querry;   // Conditions
  157.     int* querryNumbers;    // Not-empty fields in querry
  158.     KH_STRTABLE* examples; // Example variables
  159.     int* exampleNumbers;   // And numbers
  160.     int* checkedFields;    // Include this fields to ANSWER table
  161.  
  162.     KH_QUERRY(char** querries, char** examples, char* checkers,
  163.               int numOfColumnsInTable);
  164.     KH_QUERRY(KH_STRTABLE* querry, KH_STRTABLE* examples,
  165.               char* checkers, int numOfColumnsInTable)
  166.      { KH_QUERRY(querry->strings, examples->strings, checkers,
  167.           numOfColumnsInTable); }
  168.  
  169.     ~KH_QUERRY();
  170.     };
  171.  
  172.     ASK_MAN.H:
  173.  
  174. extern int indexNo; // The only reason - error in Paradox Engine
  175.  
  176. class KH_AskManager
  177.     {
  178.     protected:
  179.      KH_PXTable** tables;              // List of tables
  180.      KH_QUERRY** querryList;           // List of querries
  181.      KH_PXTable* workTable;            // Intermadiate results
  182.      KH_PXTable* answerTable;       // Result
  183.      int numberOfTables;            // Total number of tables 
  184.  
  185.     public:
  186.      KH_AskManager(char** tableNames, KH_QUERRY** querries,
  187.           int numberOfTables);
  188.  
  189.      ~KH_AskManager();
  190. Build ANSWER.DB containing all fields from all tables:
  191.      int createAnswerTable(int nOT);
  192. checkQuerry() - is the querry correct
  193.      /*virtual*/ int KH_AskManager::checkQuerry();
  194.  
  195. Service:
  196. ......................................................
  197.  
  198.      int processSingleTable();  
  199.      int processMultipleTables();
  200.      int process();             
  201.  
  202. .......................................................
  203.  
  204.     };
  205.  
  206.     Example:
  207. 1. 
  208.  
  209. #include <iostream.h>
  210. #include <time.h>
  211. /////////////////////
  212.  
  213. void main()
  214.     {
  215.     if(PXInit() != PXSUCCESS)
  216.      return;
  217.  
  218.     char* tableNames[] = { "demo.db" }; 
  219.     char* q[] = { "", "", "", "", "", "", "", "", "" };   // Querries
  220.     char* e[] = { "", "", "", "", "", "", "", "", "" };   // Examples
  221.     char* c = "\2\2\2\2\2\2\2\2\2\2\0";                   // Checkers
  222.  
  223.     KH_QUERRY* querry1 = new KH_QUERRY(q, e, c, 5);  // Build querry
  224.     KH_QUERRY* querries[2];                          // Build querry list
  225.     querries[0] = querry1;
  226.     KH_AskManager* ask = new KH_AskManager(tableNames, querries, 1);
  227.     ask->process();
  228. ////////////
  229. Next code print table contents on screen:
  230.     KH_PXTable table("answer.db");
  231.  
  232.     char* shablon[] = { "", "", "", "", "", "", "", "", "", "", "" };
  233.     char buf[255];
  234.     int eot;
  235.     while(table.Find(shablon) == 1)
  236.      {
  237.      for(int i = 2; i < 5; i++)
  238.          {
  239.          table.setFld(i);
  240.          table.TranslateField(buf);
  241.          cout << buf << '\t';
  242.          }
  243.      cout << "\n";
  244.      }
  245.  
  246.  
  247.     delete ask;
  248.     delete querry1;
  249.  
  250.     unlink("answer.db");
  251.     PXExit();
  252.     }
  253.  
  254. 2. 3 tables:
  255.  
  256. void main()
  257.     {
  258.     if(PXInit() != PXSUCCESS)
  259.         return;
  260.     indexNo = 0;     // Attention!!! Absolutely necessary!!!
  261.  
  262.     char* tableNames[] = { "demo.db", "demo.db", "demo.db" };
  263.  
  264.     char* q[] = { "", "", "", "", "", "", "", "", "" };   // Querries
  265.     char* e[] = { "", "", "X", "", "", "", "", "", "" };  // Examples
  266.     char* c = "\2\2\2\2\2\2\2\2\2\2\0";                   // Checkers
  267.  
  268.     KH_QUERRY* querry1 = new KH_QUERRY(q, e, c, 5);  // Build querry
  269.     KH_QUERRY* querries[3];                          // Querry list
  270.     querries[0] = querry1;
  271.     querries[1] = querry1;
  272.     querries[2] = querry1;
  273.     KH_AskManager* ask = new KH_AskManager(tableNames, querries, 3);
  274.  
  275.     ask->process();              // Process querry, get ANSWER table
  276.  
  277. View the result.
  278.  
  279.     KH_PXTable table("answer.db");
  280.  
  281.     char* shablon[] = { "", "", "", "", "", "", "", "", "", "", "",
  282.                         "", "", "", "", "", "", "", "", "", "", "",
  283.                         "", "", "", "" };
  284.     char buf[255];
  285.     int eot;
  286.     long l = 0;
  287.     while(table.Find(shablon) == 1)
  288.      {
  289.      for(int i = 1; i < 16; i++)
  290.          {
  291.          table.setFld(i);
  292.          table.TranslateField(buf);
  293.          cout << i << ": " << buf << "\n";
  294.          }
  295.      cout << "<<<<<<<<<<<<<  RECORD " << l << " >>>>>>>>>>>>>>>>\n";
  296.      l++;
  297.      }
  298.  
  299.  
  300.     delete ask;
  301.     delete querry1;
  302.  
  303.     unlink("answer.db");
  304.     PXExit();
  305.     }
  306.  
  307. 3. BLOBs are not standart and we do not know, how to process them
  308. in concrete situation. We use showBLOB() function call, and user 
  309. must overload it if necessary.
  310.  
  311. void main()     // Add operations on BLOB fields
  312.     {
  313. // Init PARADOX ENGINE
  314.     if(PXInit() != PXSUCCESS)
  315.      return;
  316. // Process single-table querry to DEMO.DB table
  317.     char* tableNames[] = { "demo.db" };            // Table name
  318.  
  319.     char* q[] = { "", "", "", "", "", "", "", "", "" };   // Querries
  320.     char* e[] = { "", "", "", "", "", "", "", "", "" };   // Examples
  321.     char* c = "\2\2\2\2\2\2\2\2\2\2\0";                   // Checkers
  322.  
  323.     KH_QUERRY* querry1 = new KH_QUERRY(q, e, c, 5);   // Build querry
  324.     KH_QUERRY* querries[2];                           // Querry list
  325.     querries[0] = querry1;
  326.     KH_AskManager* ask = new KH_AskManager(tableNames, querries, 1);
  327.  
  328.     ask->process();            // Process querry, get ANSWER table
  329. ////////////
  330.     KH_PXTable table("answer.db");
  331.  
  332.     char* shablon[] = { "", "", "", "", "", "", "", "", "", "", "" };
  333.     char buf[255];
  334.     char buf1[255];
  335.     int eot;
  336.     KH_PXFLD* retFld = new KH_PXFLD(buf1);
  337.     while(table.Find(shablon) == 1)
  338.      {
  339.      for(int i = 1; i < 6; i++)
  340.          {
  341.          table.setFld(i);
  342. If BLOB - message.
  343.          if(table.TranslateField(buf) == KH_BLOB)
  344.           {
  345.           table.getField(retFld);
  346.           cout << "***************** BLOB: " << retFld->str << "\n";
  347.           }
  348.          else
  349.           cout << buf << "\n";
  350.          }
  351.      cout << "<<<<<<<<<<<<<<<<<<<<<<<<\n";
  352.      }
  353.  
  354.     delete retFld;
  355.     delete ask;
  356.     delete querry1;
  357.  
  358.     unlink("answer.db");
  359.     PXExit();
  360.     }
  361.  
  362. ///////////////////////////////////////////////////////////////////////
  363. ///////////////////////////////////////////////////////////////////////
  364. Interface classes of KNOW-HOW.DATASHELL
  365.  
  366.      KH_TABLE.H - abstract table, not only Paradox:
  367.  
  368. #include "simple.h" // like ulong == unsigned long
  369.  
  370. class KH_TableView
  371.     {
  372.     protected:
  373.      ulong yStart;       // 1st visible record
  374.      int  xStart;        // Left page field
  375.      int  yPos;          // From page top to cursor
  376.      int  xPos;          // From left page side
  377.      int  xCurs;         // Current field
  378.      ulong yCurs;        // Current record
  379.      int xScreen;        // Screen clip, cells
  380.      int yScreen;        // -//-
  381.      int nColumns;       // Columns in table, -1 - autodetect
  382.      int* iColWidth;     // Column widths
  383.      int* colNumbers;    // You could rotate columns
  384.  
  385.     public:
  386.      KH_TableView(int width, int height, int columnsNumber = -1,
  387.             int* widthOfColumns = NULL, int* columnsOrder = NULL);
  388. Clip area
  389.      void setWidth(int w) { xScreen = w; }
  390.      void setHeight(int h) { yScreen = h; }
  391. Cursor move
  392.      int Left();
  393.      int Right();
  394.      int Up();
  395.      int Down();
  396.      void Home();
  397.      void End();
  398.      void PgUp();
  399.      void PgDown();
  400.      void CtrlPgUp();
  401.      void CtrlPgDown();
  402.      void PgLeft();
  403.      void PgRight();
  404. Rotate (as CTRL-R in Paradox)
  405.      void Rotate();
  406. Insert - remove record
  407.      virtual void insRecord() {}
  408.      virtual void delRecord() {}
  409. For mouse jumps
  410.      void moveToCell(loc ms);
  411.  
  412.      virtual long RecordNumber()    { return 1L; }
  413.      virtual int getColumnNumber()  { return 1; }
  414.     };
  415.  
  416.     ABSTABLE.H - window - dependent abstract table.
  417. Write access:
  418. enum KH_TABLE_MODE { KH_EDIT_TABLE, KH_VIEW_TABLE };
  419.  
  420. class KH_AbstractTable : public KH_TableView, public Window
  421.     {
  422.     protected:
  423.      KH_TABLE_MODE mode; 
  424.     public:
  425.      KH_AbstractTable(rect coordinates, char* swappingFileName = "",
  426.           char* header = "", int shadowWidthPixels = 0,
  427.           int numOfColumn = -1, int* widthsOfColumns = NULL,
  428.           int* columnsOrder = NULL,
  429. KNOW-HOW specific:
  430.           BORDERS b_type = SHOW_BORDER,
  431.           BORDERS hdr_b_type = SHOW_BORDER,
  432.           int resizeStatus = 0, int pattern = 0, int hdr_pat = 0);
  433.  
  434.      virtual void getFieldName(char* name, int n) // Field name
  435.           { strcpy(name, "Name"); }
  436.      .............................
  437.      virtual long RecordNumber()    { return 25L; }
  438.      virtual int getColumnNumber()  { return 8; }
  439. Redraw current field
  440.      virtual void showField(int xcell, int x, int y, int flag,
  441.                int field_type) {}
  442. Field type
  443.      virtual int getFieldType(int x, int y) { return 0; }
  444. Show object
  445.      virtual void show();
  446.      virtual void exe(int act = 0);
  447.      virtual int writeAccess() { return 0; }
  448. Save
  449.      virtual void saveTable() {}
  450. Edit of field
  451.      virtual void editField() {}
  452.      void showCursor(rect r, int color, int startX, int startY,
  453.                       int endX, int endY);
  454.      void getItemPos(loc l, int* startX, int* startY, int* endX,
  455.                int* endY);
  456.      virtual int getFieldMaxWidth() { return 1; }
  457. Grid
  458.      void line_table(rect r);
  459. Search, if ask == 1 - ask for value, else use field value
  460.      virtual int searchField(int ask) { return 1; }
  461.  
  462.     };
  463.  
  464. Example.
  465.  
  466. void main()
  467.     {
  468.     if(!init_KNOW_HOW())
  469.      return;
  470.     setfillstyle(SOLID_FILL, pColorSet->colors.BAK_COLOR);
  471.     bar(0, 0, getmaxx(), getmaxy());
  472.  
  473.     int wid[] = { 5, 6, 7, 8, 7, 6, 5, 6, 6, 6 };
  474.     int num[] = { 1, 0, 3, 2, 4, 5, 6, 7, 8 };
  475.     KH_AbstractTable w(rect(10, 10, 50, 20), "window.pcy",
  476.           " KNOW-HOW 4.x", 6, 8, wid, num);
  477.     w.show_window();
  478.     w.exe();
  479.     w.hide();
  480.     close_KNOW_HOW();
  481.     closegraph();
  482.     }
  483.  
  484.     KHPXTAB.H - Paradox table.
  485.  
  486. class KH_PX_Table : public KH_AbstractTable, public KH_PXTable
  487.     {
  488.     public:
  489.      KH_PX_Table(rect coordinates,             
  490.             char* tabName,                     
  491.             char* fName = "",                  
  492.             char* h = "",                      
  493.             int s = 0,                         
  494.             BORDERS b_type = SHOW_BORDER,      
  495.             BORDERS hdr_b_type = SHOW_BORDER,  
  496.             int res = 0,                       
  497.             int hdr_pat = 0,           
  498.       
  499.             int fNo = 0,               
  500.             char** fields = NULL,      
  501.             char** types = NULL);      
  502.         ~KH_PX_Table();
  503.         int check_type(int type, char* string);
  504.         virtual long RecordNumber();
  505.         virtual int getColumnNumber();
  506.         virtual void getFieldName(char* name, int n);
  507.         virtual void showField(int sx, int x, int y, int flag,
  508.             int field_type);
  509.         virtual void saveTable();
  510.         virtual int writeAccess() { return 1; }
  511.         virtual void editField();
  512.         virtual int getFieldMaxWidth();
  513.         virtual void insRecord();
  514.         virtual void delRecord();
  515.         virtual int searchField(int ask);
  516.     };
  517.  
  518. Example.
  519.  
  520. void main()
  521.     {
  522.     if(!init_KNOW_HOW())
  523.      return;
  524.     if(PXInit() != PXSUCCESS)
  525.      return;
  526.  
  527.     KH_PX_Table w(rect(20, 2, 78, 24), "demo.db", "window.pcy",
  528.      "", //" KNOW-HOW 4.x", 3, SHOW_BORDER, NO_BORDER, 0, 19);
  529.     w.show_window();
  530.     w.exe();
  531.     w.hide();
  532.  
  533.     PXExit();
  534.     close_KNOW_HOW();
  535.     closegraph();
  536.     }
  537.  
  538.     Containers. BlockT(ext)PXTable and BlockI(con)PXTable.
  539.  
  540. void main()
  541.     {
  542.     if(!init_KNOW_HOW())
  543.      return;
  544.     if(PXInit() != PXSUCCESS)
  545.      return;
  546.  
  547.     BlockIPXTable w(rect(10, 2, 78, 18), "demo.db", "window.pcy",
  548.      " KNOW-HOW 4.x", 3, SHOW_BORDER, SHOW_BORDER, MOVE | RESIZE, 19,
  549.      0);
  550.  
  551.     w.show_window();
  552.     w.exe();
  553.     w.hide();
  554.  
  555.     PXExit();
  556.     close_KNOW_HOW();
  557.     closegraph();
  558.     }
  559.  
  560. Querries. Ask edits querry.
  561. ASK.H
  562.  
  563. #define QUERRY_MAX_LEN 250
  564. #define EXAMPLE_MAX_LEN 8
  565.  
  566. class Ask : public KH_AbstractTable
  567.     {
  568.     protected:
  569.      char* tableName;              // Name
  570.      KH_STRTABLE* querry;          // Conditions
  571.      KH_STRTABLE* examples;        // Example variables
  572.      char* checkers;               // Marked
  573.  
  574.      KH_STRTABLE* fieldNames;      // Field names
  575.  
  576.     public:
  577.      Ask(rect coordinates, char* tabName, KH_STRTABLE* fields,
  578.             char* fName = "",
  579.             int s = 0, BORDERS b_type = SHOW_BORDER,
  580.             int res = 0, int pat = 0, int hdr_pat = 0);
  581.  
  582.      ~Ask() { delete tableName; delete querry; delete examples;
  583.            delete checkers; delete fieldNames; }
  584.  
  585.      virtual void getFieldName(char* name, int n);
  586.  
  587.      virtual long RecordNumber()    { return 1L; }
  588.      virtual int getColumnNumber()  { return nColumns; }
  589.  
  590.      virtual void showField(int xcell, int x, int y, int flag,
  591.                int field_type);
  592.  
  593.      virtual void show();
  594.      virtual int writeAccess() { return 1; }
  595.      virtual void saveTable() {}
  596.  
  597.      virtual void editField();
  598.      virtual int searchField(int ask);
  599.      virtual int getFieldMaxWidth();
  600.  
  601. toush() function return (in global) results of edit session. See ASK.H.
  602.  
  603.      virtual void touch();
  604.     };
  605.  
  606. Example.
  607.  
  608. void main()
  609.     {
  610.     if(!init_KNOW_HOW())
  611.      return;
  612.  
  613.     char* s[] = { "One", "Two", "Three", "Four", "Five" };
  614.     KH_STRTABLE* fields = new KH_STRTABLE(5, s);
  615.  
  616.     Ask w(rect(10, 10, 50, 13), "demo.db", fields, "window.pcy", 0);
  617.  
  618.     w.show_window();
  619.     w.exe();
  620.     w.hide();
  621.  
  622.     close_KNOW_HOW();
  623.     closegraph();
  624.     }
  625.  
  626.